package com.icongyou.enterprise.talent_analysis.listener;

import com.icongyou.enterprise.talent_analysis.event.TemplateChangeEvent;
import com.icongyou.enterprise.talent_analysis.mapper.PortraitUpdateTaskMapper;
import com.icongyou.enterprise.talent_analysis.service.PortraitCacheService;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Arrays;
import java.util.Collections;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;

/**
 * 画像模板变更监听器测试
 */
@ExtendWith(MockitoExtension.class)
@DisplayName("画像模板变更监听器测试")
class PortraitTemplateChangeListenerTest {

    @Mock
    private PortraitCacheService cacheService;

    @Mock
    private PortraitUpdateTaskMapper taskMapper;

    @InjectMocks
    private PortraitTemplateChangeListener listener;

    @Test
    @DisplayName("handleTemplateChange - 清除旧模板缓存并创建更新任务")
    void testHandleTemplateChange_Success() {
        // Arrange
        TemplateChangeEvent event = new TemplateChangeEvent(
            this, "880e8400-e29b-41d4-a716-446655440401", "550e8400-e29b-41d4-a716-446655440003", 
            "old-template", "new-template",
            Arrays.asList("660e8400-e29b-41d4-a716-446655440105", "660e8400-e29b-41d4-a716-446655440103")
        );
        
        doNothing().when(cacheService).invalidateTemplateCache("old-template");
        when(taskMapper.insert(any())).thenReturn(1);

        // Act
        listener.handleTemplateChange(event);

        // Assert
        verify(cacheService).invalidateTemplateCache("old-template");
        verify(taskMapper, times(2)).insert(any()); // 2个学生
    }

    @Test
    @DisplayName("handleTemplateChange - 旧模板为null不清除缓存")
    void testHandleTemplateChange_NullOldTemplate() {
        // Arrange
        TemplateChangeEvent event = new TemplateChangeEvent(
            this, "880e8400-e29b-41d4-a716-446655440401", "550e8400-e29b-41d4-a716-446655440003", 
            null, "new-template",
            Arrays.asList("660e8400-e29b-41d4-a716-446655440105")
        );
        
        when(taskMapper.insert(any())).thenReturn(1);

        // Act
        listener.handleTemplateChange(event);

        // Assert
        verify(cacheService, never()).invalidateTemplateCache(anyString());
        verify(taskMapper).insert(any());
    }

    @Test
    @DisplayName("handleTemplateChange - 无学生列表不创建任务")
    void testHandleTemplateChange_NoStudentList() {
        // Arrange
        TemplateChangeEvent event = new TemplateChangeEvent(
            this, "880e8400-e29b-41d4-a716-446655440401", "550e8400-e29b-41d4-a716-446655440003", 
            "old-template", "new-template",
            Collections.emptyList()
        );
        
        doNothing().when(cacheService).invalidateTemplateCache("old-template");

        // Act
        listener.handleTemplateChange(event);

        // Assert
        verify(cacheService).invalidateTemplateCache("old-template");
        verify(taskMapper, never()).insert(any());
    }

    @Test
    @DisplayName("handleTemplateChange - 单个任务创建失败不影响其他")
    void testHandleTemplateChange_OneTaskFails() {
        // Arrange
        TemplateChangeEvent event = new TemplateChangeEvent(
            this, "880e8400-e29b-41d4-a716-446655440401", "550e8400-e29b-41d4-a716-446655440003", 
            "old-template", "new-template",
            Arrays.asList("660e8400-e29b-41d4-a716-446655440105", "660e8400-e29b-41d4-a716-446655440103", "660e8400-e29b-41d4-a716-446655440101")
        );
        
        doNothing().when(cacheService).invalidateTemplateCache("old-template");
        when(taskMapper.insert(any()))
            .thenReturn(1)
            .thenThrow(new RuntimeException("插入失败"))
            .thenReturn(1);

        // Act
        listener.handleTemplateChange(event);

        // Assert
        verify(taskMapper, times(3)).insert(any());
    }

    @Test
    @DisplayName("handleTemplateChange - 整体异常不中断")
    void testHandleTemplateChange_Exception() {
        // Arrange
        TemplateChangeEvent event = new TemplateChangeEvent(
            this, "880e8400-e29b-41d4-a716-446655440401", "550e8400-e29b-41d4-a716-446655440003", 
            "old-template", "new-template",
            Arrays.asList("660e8400-e29b-41d4-a716-446655440105")
        );
        
        doThrow(new RuntimeException("缓存失效失败"))
            .when(cacheService).invalidateTemplateCache("old-template");

        // Act - 不应抛出异常
        listener.handleTemplateChange(event);

        // Assert
        verify(cacheService).invalidateTemplateCache("old-template");
    }
}
